package com.tos.second.controller;

import com.tos.second.common.Result.PageResult;
import com.tos.second.common.Result.Result;
import com.tos.second.dto.AdminUserEditDto;
import com.tos.second.dto.UserAddDto;
import com.tos.second.dto.UserEditDto;
import com.tos.second.dto.UserPageQueryDTO;
import com.tos.second.entity.User;
import com.tos.second.service.AdminService;
import com.tos.second.service.UserService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;


    /**
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(UserPageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询");
        PageResult pageResult = adminService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改用户状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable Integer status,Long id) {
        log.info("修改用户状态：{},{}", id, status);
        adminService.setStatus(id, status);
        return Result.success("修改状态成功");
    }

    /**
     * 新增管理员
     * @param userAddDto
     * @return
     */
    @PostMapping("/addAdmin")
    public Result<String> addAdmin(@RequestBody UserAddDto userAddDto) {
        log.info("新增管理员：{}", userAddDto);
        adminService.saveUser(userAddDto);
        return Result.success("添加成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result<String> deleteUserById(@PathVariable Integer id) {
        log.info("根据id删除用户：{}", id);
        adminService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<User> selectById(@PathVariable Long id) {
        log.info("根据id查询用户：{}", id);
        User user = adminService.getById(id);
        return Result.success(user);
    }

    /**
     * 编辑员工信息
     * @param editDto
     * @return
     */
    @PostMapping("/change")
    public Result<String> updateUser(@RequestBody AdminUserEditDto editDto) {
        log.info("编辑员工：{}", editDto);
        adminService.updateUser(editDto);
        return Result.success("修改成功");
    }

}
